package com.cfs.daoimpl.sys;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;

import com.cfs.po.sys.Right;
import com.cfs.vo.Pager;


/**
 *	角色管理dao
 *	@author wang.yujie
 * 	@since 2012-12-23
 *	@version 1.0
 */
public class RightDao extends Dao {

	private RowMapper<Right> mapper = new RowMapper<Right>() {
		@Override
		public Right mapRow(ResultSet rs, int rowNum) throws SQLException {
			Right right = new Right();
			right.setId(rs.getLong("ID"));
			right.setName(rs.getString("NAME"));
			right.setUri("URI");
			return right;
		}
	};

	/**
	 * 权限批量添加
	 */
	public int add(Right[] rights) {
		List<Object[]> paramList = new ArrayList<Object[]>();
		for(Right right : rights) {
			paramList.add(new Object[] {right.getName(), right.getUri()});
		}

		int[] rows = jdbcTemplate.batchUpdate(DB_SQL.RIGHT_INS, paramList);

		System.out.println("role 批量添加  执行:" + rows.length);
		return rows.length;
	}
	
	/**
	 * 权限批量更新
	 */
	public int update(Right[] rights) {
		
		List<Object[]> pList = new ArrayList<Object[]>();
		for(Right r : rights){
			pList.add(new Object[]{r.getName(), r.getUri(), r.getId()});
		}

		int[] rows = jdbcTemplate.batchUpdate(DB_SQL.RIGHT_UPD_BY_ID, pList);

		System.out.println("right update 执行:" + rows.length);
		return rows.length;
	}
	
	/**
	 * 权限删除
	 */
	public int delete(long[] ids) {
		List<Object[]> paramList = new ArrayList<Object[]>();
		for(long id : ids) {
			paramList.add(new Object[]{id});
		}
		int[] rows = jdbcTemplate.batchUpdate(DB_SQL.RIGHT_DEL_BY_ID, paramList);

		System.out.println("right 执行删除：" + rows.length);
		return rows.length;
	}
	
	/**
	 * 根据rid获取角色
	 * @param rid
	 * @return
	 */
	public Right query(Long rid) {
		List<Right> ra = jdbcTemplate.query(DB_SQL.ROLE_QUERY_BY_ID, mapper, rid);
		
		return ra==null || ra.size()==0?null:ra.get(0);
	}
	
	/**
	 * 动态条件查询
	 * @param c
	 * @return
	 */
	public List<Right> queryAll(Pager p){
		//查询总条数
		int count = jdbcTemplate.queryForInt("SELECT COUNT(1) FROM TB_RIGHT");
		p.resetTotalR(count);

		List<Right> list = jdbcTemplate.query(DB_SQL.getPageSQL(DB_SQL.RIGHT_QUERY_ALL), mapper);

		return list;
	}

	// 角色权限维护
	public int updateRight(Long roleId, List<Right> rights){
		List<Object[]> upList = new ArrayList<Object[]>();
		List<Object[]> addList = new ArrayList<Object[]>();

		List<Object[]> ids = jdbcTemplate.query(DB_SQL.ROLE_RIGHT_QUERY_BY_ROLE, new Object[]{roleId}, new RowMapper<Object[]>(){
			@Override
			public Object[] mapRow(ResultSet rs, int arg1) throws SQLException {
				return new Object[]{rs.getLong(1),rs.getLong(2)};
			}});

		for(Right r : rights){
			if(ids.size()>0){
				Object[] id = ids.remove(0);
				upList.add(new Object[]{r.getId(), id[0], id[1]});
			}else{
				addList.add(new Object[]{roleId, r.getId()});
			}
		}
		
		int[] a,b,c;a = b = c = new int[0];
		if(upList.size()>0){
			a = jdbcTemplate.batchUpdate(DB_SQL.ROLE_RIGHT_UPD_RIGHT, upList);
		}
		if(addList.size()>0){
			b = jdbcTemplate.batchUpdate(DB_SQL.ROLE_RIGHT_ADD, addList);
		}
		if(ids.size()>0){
			c = jdbcTemplate.batchUpdate(DB_SQL.ROLE_RIGHT_DEL, ids);
		}

		return a.length + b.length + c.length;
	}
}
